package com.estronger.shopmanage.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.ui.order.bean.BitmapItem;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.soonfor.evaluate.base.EvalBaseAdapter;
import com.soonfor.evaluate.tools.ImageUtils;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by DingYG on 2017/1/17 0017.
 */

public class PhotoRecyclerAdapter extends EvalBaseAdapter<PhotoRecyclerAdapter.ViewHolder, BitmapItem> {

    protected Context context;
    protected LayoutInflater inflater;
    protected List<LocalMedia> myList = new ArrayList<LocalMedia>();
    protected PhotosAdaterCallback callback;
    private int maxSize = 10;
    private boolean ifCanDelete = true;//是否可删除

    public PhotoRecyclerAdapter(Activity context) {
        super(context);
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.view_photos_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setData(position);
    }

    public void setIfCanDelete(boolean canDelete) {
        this.ifCanDelete = canDelete;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public List<LocalMedia> getMyList() {
        return myList;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public int getItemCount() {
        if(myList ==null){
            return 0;
        }else if(myList.size() > maxSize){
            return maxSize;
        }
        return myList.size();
    }

    @Override
    public void notifyDataSetChanged(List dataList) {
    }

    public void notifyDataSetChanged(List<LocalMedia> dataList, boolean ifClear) {
        if (myList == null) myList = new ArrayList<>();
        else if (ifClear) myList.clear();
        if (dataList == null) dataList = new ArrayList<>();
        myList.addAll(dataList);
        notifyDataSetChanged();
    }
    public void notifyDataSetChanged(LocalMedia bitmapItem, boolean ifClear) {
        if (myList == null) myList = new ArrayList<>();
        else if (ifClear) myList.clear();
        myList.add(bitmapItem);
        notifyDataSetChanged();
    }

    public void setPhotosAdaterCallback(PhotosAdaterCallback callback) {
        this.callback = callback;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgf;
        private ImageView imgfDelete;
        public ViewHolder(View view){
            super(view);
            this.imgf = (ImageView) view.findViewById(R.id.imgfCpBitmap);
            this.imgfDelete = (ImageView) view.findViewById(R.id.imgfDelete);
        }
        public void setData(final int position) {
            final LocalMedia bitmapItem = myList.get(position);
            try {
                if (PictureMimeType.isPictureType(bitmapItem.getMimeType()) == PictureConfig.TYPE_VIDEO){//显示视频的缩略图
                    if (TextUtils.isEmpty(bitmapItem.getCompressPath())){//没有缩略图则获取
                        String path = TextUtils.isEmpty(bitmapItem.getAndroidQToPath())?bitmapItem.getPath():bitmapItem.getAndroidQToPath();
                        Bitmap bitmap = ImageUtils.getVideoThumbnail(path,
                                100,100,
                                MediaStore.Images.Thumbnails.MINI_KIND);
                        imgf.setImageBitmap(bitmap);
                    }else {
                        //加载图片
                        Picasso.with(context)
                                .load(bitmapItem.getCompressPath())
                                .placeholder(R.drawable.no_picture_icon)
                                .error(R.drawable.no_picture_icon).into(imgf);
                    }

                }else {
                    if (bitmapItem.getPath().startsWith("http://")||bitmapItem.getPath().startsWith("https://")){
                        //加载图片
                        Picasso.with(context)
                                .load(bitmapItem.getPath())
                                .placeholder(R.drawable.no_picture_icon)
                                .error(R.drawable.no_picture_icon).into(imgf);
                    }else {
                        //本地文件
                        File file = null;
                        if(bitmapItem.getCompressPath()!=null){
                            file = new File(bitmapItem.getCompressPath());
                        }
                        //加载图片
                        Picasso.with(context).load(file).placeholder(R.drawable.no_picture_icon).
                                error(R.drawable.no_picture_icon).into(imgf);
                    }
                }
            }catch (Exception e){
                e.fillInStackTrace();
            }
            if(!ifCanDelete){
                imgfDelete.setVisibility(View.GONE);
            }else{
                imgfDelete.setVisibility(View.VISIBLE);
                imgfDelete.setTag(position);
                imgfDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int pos = (int) view.getTag();
                        if (pos >=0 && pos < myList.size() && callback != null) {
                           callback.delete(pos);
                        }
                    }
                });
            }
            imgf.setTag(position);
            imgf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        if (PictureMimeType.isPictureType(bitmapItem.getMimeType()) == PictureConfig.TYPE_VIDEO){//增加播放视频
                            PictureSelector.create((Activity) context).externalPictureVideo(bitmapItem.getPath());
                        }else {
                            ArrayList<String> previews = new ArrayList<>();
                            for (LocalMedia localMedia : myList) {
                                if (TextUtils.isEmpty(localMedia.getPath())) {
                                    previews.add(localMedia.getCompressPath());
                                }else previews.add(localMedia.getPath());
                            }
                            int pos = (int) view.getTag();
                            if (pos < 0 || pos > myList.size())  pos = 0;
                            if (callback != null) callback.preview(previews, position);
                        }
                    }catch (Exception e){
                        e.fillInStackTrace();
                    }
                }
            });
        }
    }

    public interface PhotosAdaterCallback{
        void preview(ArrayList<String> picList, int position);//预览
        void delete(int position);//删除
    }
}
